#!/bin/bash

# 启动Molecule Scope应用 - 服务器部署版本
echo "🚀 Starting Molecule Scope Application (Server Deployment)..."

# 设置部署环境变量
export DEPLOYMENT_ENV="production"
export SERVER_URL="http://office20.inoteexpress.com"
export BACKEND_PORT=8001
export FRONTEND_PORT=8002

# 检查是否安装了Python和Node.js
if ! command -v python &> /dev/null && ! command -v python3 &> /dev/null; then
    echo "❌ Python is not installed. Please install Python 3.8+"
    exit 1
fi

if ! command -v node &> /dev/null; then
    echo "❌ Node.js is not installed. Please install Node.js 16+"
    exit 1
fi

# 设置生产环境变量
export VITE_API_BASE_URL="${SERVER_URL}:${BACKEND_PORT}"

# 启动后端服务
echo "📦 Starting FastAPI backend server on port ${BACKEND_PORT}..."
cd "$(dirname "$0")"
python -m uvicorn main:app --host 0.0.0.0 --port ${BACKEND_PORT} &
BACKEND_PID=$!

# 等待后端启动
echo "⏳ Waiting for backend to start..."
sleep 5

# 启动前端服务
echo "🌐 Starting Vue.js frontend server on port ${FRONTEND_PORT}..."
cd client
VITE_API_BASE_URL="${SERVER_URL}:${BACKEND_PORT}" npm run dev -- --port ${FRONTEND_PORT} &
FRONTEND_PID=$!

echo ""
echo "✅ Application started successfully!"
echo "📊 Backend API: ${SERVER_URL}:${BACKEND_PORT}"
echo "🎨 Frontend App: ${SERVER_URL}:${FRONTEND_PORT}"
echo "📚 API Docs: ${SERVER_URL}:${BACKEND_PORT}/docs"
echo ""
echo "Press Ctrl+C to stop both servers"

# 等待用户中断
wait $BACKEND_PID $FRONTEND_PID
